package com.yunfeisoft.base.controller;

import com.yunfeisoft.base.model.User;
import com.yunfeisoft.base.model.WorkflowInstance;
import com.yunfeisoft.base.service.inter.WorkflowInstanceService;
import com.yunfeisoft.common.base.BaseController;
import com.yunfeisoft.common.utils.Page;
import com.yunfeisoft.common.utils.Response;
import com.yunfeisoft.common.utils.ResponseUtils;
import com.yunfeisoft.utils.ApiUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: WorkflowInstanceController
 * Description: 流程实例Controller
 * Author: Jackie liu
 * Date: 2020-01-03
 */
@RestController
public class WorkflowInstanceController extends BaseController {

    @Autowired
    private WorkflowInstanceService workflowInstanceService;

    /**
     * 分页查询流程实例
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/workflowInstance/list", method = {RequestMethod.POST, RequestMethod.GET})
    public Response list(HttpServletRequest request, HttpServletResponse response) {
        String title = ServletRequestUtils.getStringParameter(request, "title", null);
        int status = ServletRequestUtils.getIntParameter(request, "status", -1);

        User loginUser = ApiUtils.getLoginUser();

        Map<String, Object> params = new HashMap<String, Object>();
        initParams(params, request);
        params.put("createId", loginUser.getId());
        params.put("title", title);
        if (status > 0) {
            params.put("status", status);
        }

        Page<WorkflowInstance> page = workflowInstanceService.queryPage(params);
        return ResponseUtils.success(page);
    }

    /**
     * 批量删除流程实例
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/workflowInstance/delete", method = {RequestMethod.POST, RequestMethod.GET})
    public Response delete(HttpServletRequest request, HttpServletResponse response) {
        String ids = ServletRequestUtils.getStringParameter(request, "ids", null);
        if (StringUtils.isBlank(ids)) {
            return ResponseUtils.warn("参数错误");
        }
        workflowInstanceService.remove(ids.split(","));
        return ResponseUtils.success("删除成功");
    }
}
